package com.diver.flinkdemo.mapper.dorisMapper;
import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.diver.flinkdemo.dto.xxt.NewRecordInfo;
import com.diver.flinkdemo.dto.xxt.PaceRecordInfo;
import com.diver.flinkdemo.entity.xxt.LocationInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

//@InterceptorIgnore(tenantLine = "true")
@SqlParser(filter = true)
//@DS("doris")
public interface LocationInfoMapper extends BaseMapper<LocationInfo> {

    @Select("select id,student_card_id,upload_time,lat,lon,address,mac,exception_msg,is_safety,safety_name, is_danger, gps_effective from location_info where student_card_id=#{studentCardId} " +
            "and upload_time BETWEEN #{currentTime} and #{currentTime}+86400000  and (lat is not null and lat != '' and lat !='0' and lon is not null and lon != '' and lon!='0') order by upload_time desc limit 0,10000")
    List<LocationInfo> getLocationInfosByStuCardId(
            @Param("studentCardId") String studentCardId, @Param("currentTime") Long currentTime);

    @Select("select student_card_id,upload_time,lat,lon,address,mac,exception_msg,is_safety,safety_name, is_danger, electricity from location_info where student_card_id=#{studentCardId} " +
            "and (lat is not null and lat != '' and lat !='0' and lon is not null and lon != '' and lon!='0') and upload_time <= unix_timestamp()*1000 order by upload_time desc limit 0,1")
    List<LocationInfo> getLocationInfoNewByStuCardId(
            @Param("studentCardId") String studentCardId);

    @Select("select lat latitude,lon longitude,upload_time timeStamp,exception_msg tips from location_info where student_card_id=#{studentCardId}  " +
            "and TO_DAYS(DATE_FORMAT(FROM_UNIXTIME(upload_time/1000), '%Y.%m.%d')) = TO_DAYS(now()) order by upload_time desc  limit 1")
    NewRecordInfo getNewRecordingByStudentCardId(@Param("studentCardId") String studentCardId);


    @Select("select electricity from location_info " +
            "where student_card_id=#{studentCardId} and electricity is not null and electricity != '' and upload_time <= unix_timestamp()*1000 order by upload_time desc limit 1")
    String getElectricityNewByStuCardId(@Param("studentCardId") String studentCardId);


    @Select("select electricity from location_info " +
            "where student_card_id=#{studentCardId} and electricity is not null and electricity != '' and electricity != '-1' order by upload_time desc limit 1")
    String getElectricityNewByStuCardIdISLINGMENG(@Param("studentCardId") String studentCardId);

    @Update("update location_info set commend_id=null where commend_id=#{commendId}")
    int removeCommendId(@Param("commendId") String commendId);

    int batchUpdateCommendIdById(@Param("idList") List<String> idList, @Param("commendId") String commendId);

    @Select("select imei, lat, lon, is_safety, safety_name, fence_id from location_info where stamp<#{stamp} and imei=#{studentCardId} order by stamp desc limit 0,1")
    LocationInfo getPreLocationInfo(@Param("stamp") long stamp, @Param("studentCardId") String studentCardId);

    @Delete("delete from location_info where student_card_id=#{studentCardId}")
    int delByStudentCardId(String studentCardId);

    @Select("select SUBSTRING_INDEX(group_concat(step Order BY ttime desc),',',1) step,DATE_FORMAT(FROM_UNIXTIME(ttime/1000), '%Y-%m-%d') timeStampd from " +
            "(select  step,upload_time ttime from location_info where student_card_id=#{studentCardId and step != '-1'} and upload_time BETWEEN #{from_time} and #{to_time}+86400000  order by ttime desc) ass  " +
            "group by timeStampd  " +
            "order by timeStampd desc")
    List<PaceRecordInfo> getPaceRecordingByStudentCardId(@Param("studentCardId") String studentCardId,
                                                         @Param("from_time") Long from_time,
                                                         @Param("to_time") Long to_time);

    @Select("select count(student_card_id) from location_info where student_card_id=#{studentCardId} " +
            "and upload_time BETWEEN #{yesterTime} and #{yesterTime}+86400000 ")
    long getReportDataUpdateNormal(@Param("studentCardId")String studentCardId, @Param("yesterTime")long yesterTime);

    @Select("select upload_time from location_info where student_card_id=#{studentCardId} order by upload_time asc  limit 1")
    Long getReportDataUpdateNumber(@Param("studentCardId")String studentCardId,  @Param("yesterTime")long yesterTime);

    List<LocationInfo> getNewestPositions(@Param("studentCardIdList") List<String> studentCardIdList,@Param("uploadTimeStart") String uploadTimeStart,@Param("uploadTimeEnd") String uploadTimeEnd);
//    @Update("update location_info set ")
    int UpdateLocationInfo(LocationInfo locationInfo);
}
